package com.cwvs.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

	public static String TimestampToString(Timestamp time) {
		String tsStr = "";
		DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		try {
			tsStr = sdf.format(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tsStr;
	}

	public static Timestamp StringToTimestamp(String time) {
		try {
			Timestamp ts = Timestamp.valueOf(time);
			return ts;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date StringToXDate(String time) {
		Date date = new Date();
		// 注意format的格式要与日期String的格式相匹配
		DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		try {
			date = sdf.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date StringToDDate(String time) {
		Date date = new Date();
		// 注意format的格式要与日期String的格式相匹配
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = sdf.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String DateToXString(Date date) {
		String dateStr = "";
		// format的格式可以任意
		DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			dateStr = sdf2.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateStr;
	}

	public static String DateToDString(Date date) {
		String dateStr = "";
		// format的格式可以任意
		DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		try {
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateStr;
	}
	/**
	 * 返回14位的时间20150402111102
	 * @param date
	 * @return
	 */
	public static long DateToLong14(Date date) {
		String dateStr = "";
		DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			dateStr = sdf.format(date);
			return Long.parseLong(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0l;
	}

//	public static void main(String[] args) {
//		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//		// 前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
//		java.util.Date dt = new Date(794309100 * 1000000);
//		String sDateTime = sdf.format(dt); // 得到精确到秒的表示：08/31/2006 21:08:00
//		System.out.println(sDateTime);
//	}
}
